package com.jcli.fx3u;

import com.jcli.HslCommunication.Core.Types.OperateResult;
import com.jcli.HslCommunication.Profinet.Melsec.MelsecA1ENet;

import java.util.Arrays;

public class PLCDemo {
    public static void main(String[] args) {
        // 创建Melsec A1E客户端对象并与PLC通信
        MelsecA1ENet melsec_net = new MelsecA1ENet("192.168.1.11", 3000);
        melsec_net.SetPersistentConnection();

        // 使用 melsec_net.ReadInt32 方法读取 D497 寄存器的值
        int d497Value = melsec_net.ReadInt32("D497").Content;

        if (d497Value == 1) {
            // 如果 D497 寄存器的值为 1，使用循环逐个读取 D500-D700 寄存器的值
            int[] d500ToD700Values = new int[201];
            for (int i = 0; i < d500ToD700Values.length; i++) {
                int[] readResult = melsec_net.ReadInt32("D" + (500 + i), (short) 1).Content;
                // 检查读取是否成功并且结果数组不为空
                if (readResult != null && readResult.length > 0) {
                    d500ToD700Values[i] = readResult[0]; // 获取数组的第一个元素作为整数
                } else {
                    // 处理读取失败的情况
                    // 可以抛出异常、记录日志或采取其他适当的操作
                }
            }
            System.out.println("D500-D700 Values: " + Arrays.toString(d500ToD700Values));

            // 将 D497 寄存器的值改写为 0
            OperateResult writeResult = melsec_net.Write("D497", 0);
            if (writeResult.IsSuccess) {
                System.out.println("D497 has been set to 0.");
            } else {
                System.out.println("Failed to write D497: " + writeResult.ToMessageShowString());
            }
    }}
}
